<?php
// Add feed links to header
add_theme_support('automatic-feed-links');

// Add custom menu support
add_theme_support( 'menus' );

// Add post thumbnail support
add_theme_support('post-thumbnails');

// Add custom background support
add_custom_background();

// Add custom navigation support
register_nav_menus(array(
	'primary-navigation' => 'Primary',
	'secondary-navigation' => '(if available) Secondary',
	'masthead' => '(if available) Masthead',
));

/* ***************************** */
/* CATCH FIRST IMAGE */
/* ***************************** */
function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  return $first_img;
}

/* ***************************** */
/* GET CURRENT CATEGORY ID */
/* ***************************** */
function current_catID() {
	global $wp_query,  $cat_obj, $currentcat;

	if (is_category()) {	
		$cat_obj = $wp_query->get_queried_object();
		$currentcat = $cat_obj->term_id;
	} 
	elseif (is_single()) {
		$category = get_the_category();
		$currentcat = $category[0]->cat_ID;
	}
	
	return $currentcat;
}

/* ***************************** */
/* LIMIT POST EXCERPT */
/* ***************************** */
function string_limit_words($string, $word_limit) {
	$words = explode(' ', $string, ($word_limit + 1));
	if(count($words) > $word_limit)
	array_pop($words);
	return implode(' ', $words);
}

/* ***************************** */
/* PAGINATION - http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin */
/* ***************************** */
function pagination($pages = "", $range = 4)
{
     $showitems = ($range * 2)+1; 
 
     global $paged;
     if(empty($paged)) $paged = 1;
 
     if($pages == "")
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }  
 
     if(1 != $pages)
     {
         echo '<div class=\'pagination\'><span>'; _e('Page','float'); echo ' ' . $paged . ' '; _e('of','float'); echo ' ' . $pages.'</span>';
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) {
			echo '<a href="'.get_pagenum_link(1).'">&laquo; '; _e('First', 'float'); echo '</a>';
		}
         if($paged > 1 && $showitems < $pages) {
			echo '<a href="'.get_pagenum_link($paged - 1).'">&lsaquo; '; _e('Previous', 'float'); echo '</a>';
		}
 
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? '<span class=\'current\'>'.$i.'</span>':'<a href="'.get_pagenum_link($i).'" class=\'inactive\'>'.$i.'</a>';
             }
         }
 
         if ($paged < $pages && $showitems < $pages) { 
			echo '<a href=\''.get_pagenum_link($paged + 1).'\'>'; _e('Next', 'float'); echo ' &rsaquo;</a>'; 
		}
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) {
			echo '<a href="'.get_pagenum_link($pages).'">'; _e('Last', 'float'); echo ' &raquo;</a>'; 
		}
         echo '</div>';
     }
}